# -*- coding: utf-8 -*-

"""
@Time        : 2020/9/11
@Author      : 你猜
@File        : froms
@Description : 
"""
from django import forms
from django.core.validators import RegexValidator,ValidationError
from store.models import Seller
#自定义验证器1
class MyValidator:
    def __call__(self, value):
        """
        自定义验证
        ：param value 表示要验证的数据
        ：:return 如果ValidationError表示验证失败正常结束表示验证通过
        """
        datas=["sb","xx"]
        for data in datas:
            if value.find(data)!=-1:
                raise ValidationError("用户名不能含有敏感词汇")
# 自定义验证器2
def my_validator(value):
    datas = ["sb", "xx"]
    for data in datas:
        if value.find(data) != -1:
            raise ValidationError("用户名不能含有敏感词汇")

class MyValidator1:
    def __call__(self, username):
        if username:
            seller=Seller.objects.filter(name=username)
            if seller:
                raise ValidationError("用户名已存在")

class LoginForm(forms.Form):
    """登录验证表单类"""
    name=forms.CharField(
        required=True,
        validators=[
            RegexValidator(r"^[a-zA-Z0-9_]{4,10}$", "只能是长度为4-10的数字字母下划线"),
            my_validator,

        ],
        error_messages={
            "required":"用户名必填",

        },
    )
    password = forms.CharField(
        required=True,
        error_messages={
            "required": "密码必填",
        }
    )


class RegisterForm(forms.Form):
    """注册验证表单类"""
    name=forms.CharField(
        required=True,
        max_length=10,
        min_length=4,
        validators=[
            RegexValidator(r"^[a-zA-Z0-9_]{4,10}$","只能是长度为4-10的数字字母下划线"),
            my_validator,
            MyValidator1(),
        ],
        error_messages={
            "required":"用户名必填",
            "max_length":"用户名不能超过10",
            "min_length":"用户名至少是4位",
        },
    )
    password = forms.CharField(
        required=True,
        max_length=8,
        min_length=4,
        error_messages={
            "required": "密码必填",
            "max_length": "密码不能超过8",
            "min_length": "用密码至少是4位"
        }
    )